#! /usr/bin/env python

import os, sys
import pygame
#import level001
import basicSprite
from pygame.locals import *
from helpers import *
from wizzardSprite import *
from textgameprog import *
from rotatingMenu import *
from spellSprite import *

pathtextset = ["poly", "cos", "mix", "const"]
coefftextset = ["1 coeff", "2 coeffs", "3 coeffs", "4 coeffs"]
numtextset = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
cotypetextset = ["poly coeffs", "sin/cos coeffs"]
clock = pygame.time.Clock()
fpslimit = 60

class spellMenu:
    
    def __init__(self, currentWizzard, surface):
        self.currentWizzard = currentWizzard
        self.cWizX = currentWizzard.rect.centerx
        self.cWizY = currentWizzard.rect.centery
        if self.cWizX >= surface.get_width()/2:
            # Right side of the screen
            defAngle = pi/2.0
        else:
            # Left side of the screen
            defAngle = 3*pi/2.0
        self.pathrotmen = RotatingMenu(self.cWizX, self.cWizY, 100, wrap="True", arc=pi, defaultAngle=defAngle, rotate=False, visible=False)
        self.coeffrotmen = RotatingMenu(self.cWizX, self.cWizY, 100, wrap="True", arc=pi, defaultAngle=defAngle, rotate=False, visible=False)
        self.numrotmen = RotatingMenu(self.cWizX, self.cWizY, 100, wrap="True", arc=pi, defaultAngle=defAngle, rotate=False, visible=False)
        self.cotyperotmen = RotatingMenu(self.cWizX, self.cWizY, 100, wrap="True", arc=pi, defaultAngle=defAngle, rotate=False, visible=False)
        self.menus = [self.pathrotmen, self.coeffrotmen, self.numrotmen, self.cotyperotmen]
        self.menuIndex = 0
        self.polycoeffs = []
        self.polychoose = "False"
        self.cossin = "False"
        self.mix = "False"
        self.const = "False"
        self.sincoscoeff = 0
        self.polycoeffs = []
        self.typeof = ""
        self.spell_cast = False

    def start1(self):
        """returns list of menu spritegroups""" 
        for i in range (len(pathtextset)):
           self.pathrotmen.addItem(MenuItem(pathtextset[i]))
        for i in range (len(coefftextset)):
            self.coeffrotmen.addItem(MenuItem(coefftextset[i]))
        for i in range (len(numtextset)):
            self.numrotmen.addItem(MenuItem(numtextset[i]))
        for i in range (len(cotypetextset)):
            self.cotyperotmen.addItem(MenuItem(cotypetextset[i]))
        self.menu = self.menus[self.menuIndex]
        self.menu.visible = True
        self.menu.update()
        return (self.pathrotmen.itemSprites,
               self.coeffrotmen.itemSprites,
               self.numrotmen.itemSprites,
               self.cotyperotmen.itemSprites)

    def stop1(self, success):
        for menu in self.menus:
            menu.close()
        if success: # spell was cast
            pygame.event.post(pygame.event.Event(FOCUS_CHANGE, focus="nextWizzard"))    # ends turn
        else:
            pygame.event.post(pygame.event.Event(FOCUS_CHANGE, focus="curWizzard"))
    
    def MoveKeyDown(self, key):
        if key == K_UP:
            self.menu.selectNextItem()
        elif key == K_DOWN:
            self.menu.selectPrevItem()

    def MoveKeyUp(self, key):
        if key == K_RETURN:
            if self.chooseItem():
                self.currentWizzard.cast(self.sincoscoeff, self.polycoeffs, self.typeof)
                self.stop1(True)
            self.changeMenu()
        elif key == K_ESCAPE:
            self.menuIndex -= 1
            if self.menuIndex < 0:
                self.stop1(False)
            else:
                self.changeMenu()

    def changeMenu(self, menuIndex=False):
        if not menuIndex:
            menuIndex = self.menuIndex
        self.menu.visible = False
        self.menu = self.menus[menuIndex]
        self.menu.visible = True
        self.menu.update()

    def chooseItem(self):
        """
        The menu logic should go like this, I think:

        pathrotmen:
            poly->coeffrotmen->numrotmen->numrotmen (times however many coeffs)->cast
            cos->numrotmen->cast
            mix->cotyperotmen(see below)
            const->numrotmen->cast
        cotyperotmen:
            polycoeffs->coeffrotmen->numrotmen (times however many coeffs)->
    cotyperotmen if coscoeffs have not been chosen else cast
            coscoeffs->numrotmen->cotyperotmen if polycoeffs have not been chosen else cast

        This may present UI issues later.
        """
        PATHTYPE = 0    # Menu name
        POLY = 0        # PATHTYPE Menu option
        COS = 1         # PAPTHTYPE Menu option
        MIX = 2         # PATHTYPE Menu option
        CONST = 3       # PATHTYPE Menu option
        NUMCOEFFS = 1   # Menu name
        ONE = 0         # NUMCOEFFS Menu option
        TWO = 1         # NUMCOEFFS Menu option
        THREE = 2       # NUMCOEFFS Menu option
        FOUR = 3        # NUMCOEFFS Menu option
        NUMBERS = 2     # Menu name
        COEFFTYPE = 3   # Menu name
        POLYCOEFFS = 0  # COEFFTYPE Menu option
        COSCOEFFS = 1   # COEFFTYPE Menu option
        

        if self.menuIndex == PATHTYPE:
            if self.pathrotmen.selectedItemNumber == POLY:
                self.menuIndex = NUMCOEFFS
                self.cossin = False
                self.typeof = "poly"
            elif self.pathrotmen.selectedItemNumber == COS:
                self.menuIndex = NUMBERS
                self.cossin = True
                self.typeof = "cos"
            elif self.pathrotmen.selectedItemNumber == MIX:
                self.menuIndex = COEFFTYPE
                self.typeof = "mix"
            elif self.pathrotmen.selectedItemNumber == CONST:
                self.const = True
                self.typeof = "poly"
                self.numpolycoeffs = 1
                self.polychoose = True
                self.menuIndex = NUMBERS
        elif self.menuIndex == NUMCOEFFS:
            self.polychoose = True
            self.cossin = False
            if self.coeffrotmen.selectedItemNumber == ONE:
                self.npc = 1
                self.numpolycoeffs = 1
                self.menuIndex = NUMBERS
            elif self.coeffrotmen.selectedItemNumber == TWO:
                self.npc = 2
                self.numpolycoeffs = 2
                self.menuIndex = NUMBERS
            elif self.coeffrotmen.selectedItemNumber == THREE:
                self.npc = 3
                self.numpolycoeffs = 3
                self.menuIndex = NUMBERS
            elif self.coeffrotmen.selectedItemNumber == FOUR:
                self.npc = 4
                self.numpolycoeffs = 4
                self.menuIndex = NUMBERS
        elif self.menuIndex == NUMBERS:
            if self.polychoose:
                if self.numpolycoeffs >= 1:
                    coeff = self.numrotmen.selectedItemNumber+1
                    self.polycoeffs.append(coeff)
                    self.numpolycoeffs -= 1
                else:
                    return True
            elif self.cossin:
                self.sincoscoeff = self.numrotmen.selectedItemNumber+1
                return True
            else:
                error, "you made a boo-boo"
        elif self.menuIndex == COEFFTYPE:
            if self.cotyperotmen.selectedItemNumber == POLYCOEFFS:
                self.polychoose = True
                self.cossin = False
                self.menuIndex = NUMCOEFFS
            elif self.cotyperotmen.selectedItemNumber == COSCOEFFS:
                self.cossin = True
                self.polychoose = False
                self.menuIndex = NUMBERS
            elif self.cotyperotmen.selectedItemNumber == 2:
                self.mix = True

    def update(self):
        for menu in self.menus:
            menu.update()
